package br.livro.android.cap8.filtro;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.TextView;
import br.database.DBhelperCetegoria;

import com.example.mobilefala.R;
import com.expressao.TelaMenu;
import com.fragments.AtualizaSpeech;

public class TelaSplashScreenRunnable extends Activity implements Runnable, AtualizaSpeech{
	private TextView textView;
	public static final int SPLASH = 4;
	public static final String CATEGORIA = "livro";
//	Handler h;
	private br.database.Categoria cat;
	private br.database.DbHelperBotao DbHelper;
	private DBhelperCetegoria DbHelperCategoria;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		checkIntentOficial.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);

//
//		DbHelper = new br.database.DbHelperBotao(this);
//		DbHelper.open();
//		List<Botao> bs = DbHelper.recuperarTodos();
//		for (Botao c : bs) {
//			
//			DbHelper.excluir(c.getId());			
//		}
//		DbHelper.close();
		
		
		setContentView(R.layout.layout_activity_handler_splash_screen);
		textView = (TextView) findViewById(R.id.tvHandlerSplash);
		// Toast.makeText(this,
		// "Aguarde o carregamento da app...",Toast.LENGTH_SHORT).show();
		new Thread(this).start();
	}

	private boolean appInstalledOrNot(String uri) {
		PackageManager pm = getPackageManager();
		boolean app_installed = false;
		try {
			pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
			app_installed = true;
		} catch (PackageManager.NameNotFoundException e) {
			app_installed = false;
		}
		return app_installed;
	}

	@Override
	public void run() {
		textView.setText("Carregando..aguarde...");

		//depois descomentar aqui
	//	try {
//			String packageName = "com.google.android.tts";
//			boolean installed = appInstalledOrNot(packageName);
//			if (installed) {
//				// This intent will help you to launch if the package is already
//				// installed
//
//				startActivity(new Intent(this, TelaMenu.class));
//				finish();
//				return;
//
//			} else {
//
//			}
			//depois descomentar aqui
//			URL url = new URL(
//					"https://dl.dropboxusercontent.com/u/25431823/Software/com.google.android.tts-3.0.10.1047791.apk");
//			HttpURLConnection c = (HttpURLConnection) url.openConnection();
//			c.setRequestMethod("GET");
//			c.setDoOutput(true);
//			c.connect();
//
//			String PATH = Environment.getExternalStorageDirectory()
//					+ "/download/";
//			File file = new File(PATH);
//			file.mkdirs();
//			File outputFile = new File(file,
//					"com.google.android.tts-3.0.10.1047791.apk");
//			FileOutputStream fos = new FileOutputStream(outputFile);
//			InputStream is = c.getInputStream();
//
//			byte[] buffer = new byte[13312];
//			int len1 = 0;
//			while ((len1 = is.read(buffer)) != -1) {
//				fos.write(buffer, 0, len1);
//			}
//			fos.close();
//			is.close();// till here, it works fine - .apk is download to my
						// sdcard in download file

			
			//depois descomentar aqui
//			Intent intent = new Intent(Intent.ACTION_VIEW);
//			intent.setDataAndType(Uri.fromFile(new File(Environment
//					.getExternalStorageDirectory()
//					+ "/download/"
//					+ "com.google.android.tts-3.0.10.1047791.apk")),
//					"application/vnd.android.package-archive");
//			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			startActivity(intent);
			startActivity(new Intent(this, TelaMenu.class));
			finish();
//
//		} catch (IOException e) {
//			// Toast.makeText(getApplicationContext(), "Update error!",
//			// Toast.LENGTH_LONG).show();
//			Log.e("Error", "Exception thrown");
		//}
	};

}
